import 'package:hive/hive.dart';

part 'wallet_model.g.dart';

/// 钱包模型
@HiveType(typeId: 0)
class WalletModel extends HiveObject {
  @HiveField(0)
  final String id;
  
  @HiveField(1)
  final String name;
  
  @HiveField(2)
  final String address;
  
  @HiveField(3)
  final String networkId;
  
  @HiveField(4)
  final DateTime createdAt;
  
  @HiveField(5)
  final bool isActive;
  
  WalletModel({
    required this.id,
    required this.name,
    required this.address,
    required this.networkId,
    required this.createdAt,
    this.isActive = true,
  });
  
  WalletModel copyWith({
    String? id,
    String? name,
    String? address,
    String? networkId,
    DateTime? createdAt,
    bool? isActive,
  }) {
    return WalletModel(
      id: id ?? this.id,
      name: name ?? this.name,
      address: address ?? this.address,
      networkId: networkId ?? this.networkId,
      createdAt: createdAt ?? this.createdAt,
      isActive: isActive ?? this.isActive,
    );
  }
  
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'address': address,
      'networkId': networkId,
      'createdAt': createdAt.toIso8601String(),
      'isActive': isActive,
    };
  }
  
  factory WalletModel.fromJson(Map<String, dynamic> json) {
    return WalletModel(
      id: json['id'],
      name: json['name'],
      address: json['address'],
      networkId: json['networkId'],
      createdAt: DateTime.parse(json['createdAt']),
      isActive: json['isActive'] ?? true,
    );
  }
}

/// 代币模型
@HiveType(typeId: 1)
class TokenModel extends HiveObject {
  @HiveField(0)
  final String symbol;
  
  @HiveField(1)
  final String name;
  
  @HiveField(2)
  final String contractAddress;
  
  @HiveField(3)
  final int decimals;
  
  @HiveField(4)
  final String balance;
  
  @HiveField(5)
  final String? price;
  
  @HiveField(6)
  final String networkId;
  
  @HiveField(7)
  final String? logoUrl;
  
  TokenModel({
    required this.symbol,
    required this.name,
    required this.contractAddress,
    required this.decimals,
    required this.balance,
    this.price,
    required this.networkId,
    this.logoUrl,
  });
  
  TokenModel copyWith({
    String? symbol,
    String? name,
    String? contractAddress,
    int? decimals,
    String? balance,
    String? price,
    String? networkId,
    String? logoUrl,
  }) {
    return TokenModel(
      symbol: symbol ?? this.symbol,
      name: name ?? this.name,
      contractAddress: contractAddress ?? this.contractAddress,
      decimals: decimals ?? this.decimals,
      balance: balance ?? this.balance,
      price: price ?? this.price,
      networkId: networkId ?? this.networkId,
      logoUrl: logoUrl ?? this.logoUrl,
    );
  }
  
  /// 获取格式化的余额
  String get formattedBalance {
    final balanceValue = double.tryParse(balance) ?? 0.0;
    if (balanceValue == 0) return '0';
    
    if (balanceValue < 0.0001) {
      return '< 0.0001';
    } else if (balanceValue < 1) {
      return balanceValue.toStringAsFixed(6);
    } else {
      return balanceValue.toStringAsFixed(4);
    }
  }
  
  /// 获取 USD 价值
  String get usdValue {
    if (price == null) return '\$0.00';
    final priceValue = double.tryParse(price!) ?? 0.0;
    final balanceValue = double.tryParse(balance) ?? 0.0;
    final usdValue = priceValue * balanceValue;
    return '\$${usdValue.toStringAsFixed(2)}';
  }
}

/// 交易模型
@HiveType(typeId: 2)
class TransactionModel extends HiveObject {
  @HiveField(0)
  final String hash;
  
  @HiveField(1)
  final String from;
  
  @HiveField(2)
  final String to;
  
  @HiveField(3)
  final String value;
  
  @HiveField(4)
  final String gasPrice;
  
  @HiveField(5)
  final String gasUsed;
  
  @HiveField(6)
  final TransactionStatus status;
  
  @HiveField(7)
  final DateTime timestamp;
  
  @HiveField(8)
  final String networkId;
  
  @HiveField(9)
  final String? tokenSymbol;
  
  @HiveField(10)
  final String? tokenAddress;
  
  TransactionModel({
    required this.hash,
    required this.from,
    required this.to,
    required this.value,
    required this.gasPrice,
    required this.gasUsed,
    required this.status,
    required this.timestamp,
    required this.networkId,
    this.tokenSymbol,
    this.tokenAddress,
  });
  
  /// 获取交易类型
  TransactionType get type {
    // 这里可以根据 from/to 地址判断交易类型
    // 简化实现，实际应该根据业务逻辑判断
    return TransactionType.transfer;
  }
  
  /// 获取格式化的金额
  String get formattedValue {
    final valueNum = double.tryParse(value) ?? 0.0;
    if (valueNum == 0) return '0';
    
    if (valueNum < 0.0001) {
      return '< 0.0001';
    } else if (valueNum < 1) {
      return valueNum.toStringAsFixed(6);
    } else {
      return valueNum.toStringAsFixed(4);
    }
  }
  
  /// 获取交易费用
  String get transactionFee {
    final gasPriceNum = double.tryParse(gasPrice) ?? 0.0;
    final gasUsedNum = double.tryParse(gasUsed) ?? 0.0;
    final fee = gasPriceNum * gasUsedNum;
    return fee.toStringAsFixed(6);
  }
}

/// 交易状态枚举
@HiveType(typeId: 3)
enum TransactionStatus {
  @HiveField(0)
  pending,
  
  @HiveField(1)
  confirmed,
  
  @HiveField(2)
  failed,
}

/// 交易类型枚举
enum TransactionType {
  transfer,
  swap,
  contract,
  receive,
}

/// 网络状态模型
class NetworkStatus {
  final String networkId;
  final bool isConnected;
  final int blockNumber;
  final DateTime lastUpdate;
  
  NetworkStatus({
    required this.networkId,
    required this.isConnected,
    required this.blockNumber,
    required this.lastUpdate,
  });
}
